#define BLINKER_PRINT Serial
#define BLINKER_MIOT_LIGHT
#define BLINKER_WIFI
// #define Wifi_DIRECT //密码直连取消注释

#include <EEPROM.h>
#define EEPROM_OPERAT_MAX_ADDR              4096    //申请操作到的最大地址addr
#define EEPROM_LAST_R_ADDR                  2432    //颜色亮度储存首地址
#define EEPROM_LAST_G_ADDR                  EEPROM_LAST_R_ADDR+1
#define EEPROM_LAST_B_ADDR                  EEPROM_LAST_G_ADDR+1
#define EEPROM_LAST_BRIGHT_ADDR             EEPROM_LAST_B_ADDR+1
#define EEPROM_COLOR_TEMPERATURE_ADDR       EEPROM_LAST_BRIGHT_ADDR+1
#define EEPROM_RAINBOW_MODE_ADDR            EEPROM_COLOR_TEMPERATURE_ADDR+1
#define EEPROM_AU_ST_ADDR             EEPROM_RAINBOW_MODE_ADDR+1 //没用上
#define EEPROM_STATE_JD_ADDR             EEPROM_AU_ST_ADDR+1 //继电器的状态地址
#define EEPROM_STATE_power_ADDR             EEPROM_STATE_JD_ADDR+1 //电源的状态地址


#include <Blinker.h>
#include <Adafruit_NeoPixel.h>
#include <FastLED.h>
#include <WS2812FX.h>

#include<Filter.h>
#include<MegunoLink.h>

#include <DNSServer.h>//密码直连将其三个库注释
#include <ESP8266WebServer.h>
#include <CustomWiFiManager.h>

// web配网页面自定义我的图标请随便使用一个图片转base64工具转换https://tool.css-js.com/base64.html, 64*64
const char Icon[] PROGMEM = "";
char auth[] = "e1396093f962";//app中获取到的Secret Key(密钥)
char ssid[] = "JXYdsg";//wifi名
char pswd[] = "qwer1234";//wifi密码

int last_r_value=0,last_g_value=0,last_b_value=0,last_bright_value=0; //第一次初始化RGB与亮度值

unsigned char RGB_style[]={7,8,9,10,11,12,17,19,20,22,29,32,33,34,35,37,38,39,41,42,44,46,47,52};//WS2812FX比较炫酷的灯光效果
unsigned char num_=0;
int time_count=0;

uint8_t lunhuan = 0;
int aust_time = 0;
int time_lunhuan=0;

#define PIN 5   //灯带数据口 D1
#define press 0   //按键 归零 D3
#define nextpress 12   //下一个按键 D6
#define austpress 14   //固定模式按键  D5
#define STATE_LED 2 //状态指示灯
#define OUT_JD 4 //测试继电器
#define OUT_canc 13 //接灯 ， 灯灭了 说明联网了  并且能app控制 D7
// 灯带 LED 数量
#define NUMPIXELS 30  
WS2812FX ws2812fx = WS2812FX(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//---------------------------律动2 的变量
#define NOISE 670  //律动2 的变量
// 定义音频级别所需的变量
int lvl = 0, minLvl = 0, maxLvl = 40; // 根据需要调整最小值和最大值
// 实例化用于平滑原始音频信号的过滤器类
ExponentialFilter<long> ADCFilter(5,0);
//---------------------------律动2 的变量

#define TOP   (NUMPIXELS+2) // 允许最大液位稍微偏离刻度
#define BRIGHTNESS  100     // 有点暗，便于录音

/*--------------律动相关--------------*/
#define sigPin A0                // ESP8266ADC引脚接MAX9814 OUT信号引脚  GAIN接3.3V
#define LED_TYPE    WS2812       // LED灯带型号
#define COLOR_ORDER GRB
uint8_t gHue = 0;
int sig;//麦克风读取数值
int pre_si;//指示旧值
int si;//指示新值
int sig_min = 1024;//初始读取值最大小值，开机后将重新界定
int sig_max = 0;
double triger_factor = 1.1;//阈值 过滤噪音 该数值调整范围1-2 数值越高要求声音越大才能触发
int drop_dot; //初始掉落位置
unsigned long drop_time;//触发掉落时机
int interval = 0; //每次律动变化的过渡时间
int drop_interval = 0; //掉落点收缩速度100
//int maxBrightness; //  = 150
int mid = int(NUMPIXELS / 2);
int delta; //渐变量
unsigned long triger_time = 0;
unsigned long resetTime;
int randC = 130; //掉落点随机颜色初始
bool raising = true;

/*--------------律动相关--------------*/

CRGB leds[NUMPIXELS];
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

unsigned char r=0;unsigned char g=0;unsigned char b=0;
int32_t cct=0; //滑块条值全局变量
int LED_mode=0;//灯带的模式 用一个全局变量来代替
uint8_t STATE_JD = 0;
uint8_t STATE_power = 1;
int hue = 0;
uint8_t computer_mode = 4; //电脑模式初始值（从第几个模式开始是电脑模式控制的）

// 新建组件对象
BlinkerRGB RGB1("RGB");
BlinkerSlider Slider1("Sliderkey");
BlinkerButton Button1("btn-0");
BlinkerButton Button2("btn-1");
BlinkerButton Button3("btn-2");
BlinkerButton Button4("btn-3");
BlinkerButton Button5("btn-4");
BlinkerButton Button6("btn-5");
BlinkerButton Button7("btn-6");
BlinkerButton Button8("btn-7");
BlinkerButton Button9("btn-8");
BlinkerButton Button10("btn-9");
#define TEXTE_1 "textkey"
BlinkerText Text1(TEXTE_1);
                                        /*保存函数开始*/
//保存颜色亮度值
void color_bright_save(int addr){

    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址addr
    EEPROM.write(addr,last_r_value);     //给EEPROM 写入值掉电保存   
    EEPROM.write(EEPROM_LAST_G_ADDR,last_g_value);
    EEPROM.write(EEPROM_LAST_B_ADDR,last_b_value);
    EEPROM.write(EEPROM_LAST_BRIGHT_ADDR,last_bright_value);
    EEPROM.commit(); //保存更改的数据
}

//保存色温值
void color_temperature_save(int addr,int32_t value){

    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址addr
    EEPROM.write(addr,value);
    EEPROM.commit(); //保存更改的数据
}

//保存模式
void mode_save(int addr,int r_mode){

    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址addr
    EEPROM.write(addr,r_mode);     //给EEPROM 写入值掉电保存   
    EEPROM.commit(); //保存更改的数据
}

//保存继电器测试  改变传送过来的值即可实现对应保存  
void JD_save(int addr,int r_mode){

    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址addr
    EEPROM.write(addr,r_mode);     //给EEPROM 写入值掉电保存   
    EEPROM.commit(); //保存更改的数据
}

//保存亮度模式
void bright_mode_save(int addr,int b_mode){

    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址addr
    EEPROM.write(addr,b_mode);     //给EEPROM 写入值掉电保存   
    EEPROM.commit(); //保存更改的数据
}

//接受指令板载信号灯闪烁
void lamp_twinkle()
{
    Text1.print(LED_mode, computer_mode);
    digitalWrite(STATE_LED, LOW);
    delay(30);
    digitalWrite(STATE_LED, HIGH);
}
// 下命令后实验执行的函数
void JD_make()
{
    if (STATE_JD==1)
{
    digitalWrite(OUT_JD, LOW);
}
if (STATE_JD==0)
{
    digitalWrite(OUT_JD, HIGH);
}
//*******************电源**************************
if (STATE_power== 1)
{
    button6_callback("on"); 
}
if (STATE_power== 0)
{
button6_callback("off"); 
}
//*******************电源**************************
}
// 不中断模式调色、亮度、色温
void duringdeal ()
{
    if (LED_mode != 1 && LED_mode != 2 && LED_mode != 5  && LED_mode != 6 && LED_mode != 7  && LED_mode != 9  && LED_mode != 10 && LED_mode != 11 && LED_mode != 12 && LED_mode != 13 && LED_mode != 14)  //如果是模式1 那么 在模式下直接调颜色
{
    LED_mode=0;
mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
}
}

void poweroff_RGB(int R,int G,int B,int bright)   //设置关灯时候RGB颜色、亮度，不保存数值
{
 //渐变控制算法，颜色亮度变化更加柔和
    while(!( (R == last_r_value) && (G == last_g_value) \
            && (B == last_b_value) && (bright == last_bright_value) )){
        if(R != last_r_value)
            (R > last_r_value)?( ++last_r_value ):( --last_r_value );
        if(G != last_g_value)
            (G > last_g_value)?( ++last_g_value ):( --last_g_value );
        if(B != last_b_value)
            (B > last_b_value)?( ++last_b_value ):( --last_b_value );
        if(bright != last_bright_value)
            (bright > last_bright_value)?( ++last_bright_value ):( --last_bright_value );

        // 把灯条变色
        for (uint16_t i = 0; i < NUMPIXELS; i++)
        {
            pixels.setPixelColor(i,last_r_value,last_g_value,last_b_value);
        }
        // 设置亮度
        pixels.setBrightness(last_bright_value);
        // 送出显示
        pixels.show();
    }
}
                                    /*RGB设置函数*/

void SET_RGB(int R,int G,int B,int bright)   //设置RGB颜色、亮度，并且保存数值
{
    //渐变控制算法，颜色亮度变化更加柔和
    while(!( (R == last_r_value) && (G == last_g_value) \
            && (B == last_b_value) && (bright == last_bright_value) )){
        if(R != last_r_value)
            (R > last_r_value)?( ++last_r_value ):( --last_r_value );
        if(G != last_g_value)
            (G > last_g_value)?( ++last_g_value ):( --last_g_value );
        if(B != last_b_value)
            (B > last_b_value)?( ++last_b_value ):( --last_b_value );
        if(bright != last_bright_value)
            (bright > last_bright_value)?( ++last_bright_value ):( --last_bright_value );

        // 把灯条变色
        for (uint16_t i = 0; i < NUMPIXELS; i++)
        {
            pixels.setPixelColor(i,last_r_value,last_g_value,last_b_value);
        }
        // 设置亮度
        pixels.setBrightness(last_bright_value);
        // 送出显示
        pixels.show();
    }
    FastLED.setBrightness(last_bright_value);
      ws2812fx.setBrightness(last_bright_value);
    color_bright_save(EEPROM_LAST_R_ADDR);  //保存颜色、亮度值
}

// 心跳包函数
void heartbeat() {
    static int canc = 0;
    canc++;
    if (canc ==2 )
    {digitalWrite(OUT_canc, HIGH);}
    BLINKER_LOG("心跳了一下子 哈哈");
    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
    RGB1.print( EEPROM.read(EEPROM_LAST_R_ADDR) , EEPROM.read(EEPROM_LAST_G_ADDR) ,\
        EEPROM.read(EEPROM_LAST_B_ADDR) ,EEPROM.read(EEPROM_LAST_BRIGHT_ADDR));   //上传RGB亮度信息
    Slider1.print(cct);  //发送用户需要的滑动条数值及设置的颜色到APP
    if (STATE_power == 1){Button6.print("on"); BlinkerMIOT.powerState("on");}else{Button6.print("off");BlinkerMIOT.powerState("off");}//给点灯APP返回电源开关状态信号 保证APP上线就能收到信息
    Text1.print(LED_mode, computer_mode);
    
}
                                /*按键回调*/
//APP RGB颜色设置回调 包括亮度
void rgb1_callback(uint8_t r_value, uint8_t g_value, 
                    uint8_t b_value, uint8_t bright_value){

    SET_RGB(r_value,g_value,b_value,bright_value);  
    RGB1.print(last_r_value,last_g_value,last_b_value,last_bright_value);   //上传RGB亮度信息

    BLINKER_LOG("L_R value: ", last_r_value);
    BLINKER_LOG("L_G value: ", last_g_value);
    BLINKER_LOG("L_B value: ", last_b_value);
BLINKER_LOG("L_Rrightness value: ", last_bright_value); 
    duringdeal();  //不中断模式调节



    
     lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
//app向设备发信息，这里能收到 
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
   // Blinker.vibrate();
  //  uint32_t BlinkerTime = millis();
}

//APP 滑动条设置回调 色温
void slider1_callback(int32_t value){
    BLINKER_LOG("色温值:",value);
    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
    Color_temperature_conversion(value,&r,&g,&b);   //色温转RGB
    SET_RGB( r , g , b , EEPROM.read(EEPROM_LAST_BRIGHT_ADDR) );  

    color_temperature_save(EEPROM_COLOR_TEMPERATURE_ADDR,(value/43));    //保存色温值
    RGB1.print(last_r_value,last_g_value,last_b_value,last_bright_value);   //上传RGB亮度信息
    cct = value;
    Slider1.print(cct);  //发送用户需要的滑动条数值及设置的颜色到APP

duringdeal();  //不中断模式调节
     
lamp_twinkle(); //板载信号灯接受指令闪烁一下
}

//APP 按键设置回调
void button1_callback(const String & state) //日光模式
{
    BLINKER_LOG("get button state: DAY");
if(state == "press")//以固定颜色进行呼吸灯
{
       LED_mode=1;
EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
    SET_RGB(255,67,1, EEPROM.read(EEPROM_LAST_BRIGHT_ADDR));  
SET_RGB(255,67,0, EEPROM.read(EEPROM_LAST_BRIGHT_ADDR)); 
 
mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
    }
    else{
       
LED_mode=1; //以设置的颜色进行呼吸灯
    }
     lamp_twinkle(); //板载信号灯接受指令闪烁一下
}

void button2_callback(const String & state) //夜光模式
{
    BLINKER_LOG("get button state: NIGHT");
if(state == "press")    //以固定颜色进行呼吸灯
{
        LED_mode=1;
EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
      SET_RGB(201,218,254, EEPROM.read(EEPROM_LAST_BRIGHT_ADDR) );  
SET_RGB(201,218,255, EEPROM.read(EEPROM_LAST_BRIGHT_ADDR) );
mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
    }
    else{
        LED_mode=2; //以设置的颜色进行呼吸灯
    }
    
     lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
void button3_callback(const String & state) //阅读模式
{
BLINKER_LOG("get button state: READING");
LED_mode=3;
mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
void button4_callback(const String & state) 
{
    BLINKER_LOG("get button state: 小爱的彩光模式");
    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
  
  EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR 
    STATE_JD = !STATE_JD;
    JD_save(EEPROM_STATE_JD_ADDR,STATE_JD);
   JD_make();//执行实验函数
  
   // SET_RGB(255,0,87, EEPROM.read(EEPROM_LAST_BRIGHT_ADDR) );
//LED_mode=0;
//mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
     lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
void button5_callback(const String & state) //温馨模式 JD
{
   // BLINKER_LOG("get button state: 温馨");
   // EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR 
    //STATE_JD = !STATE_JD;
    //JD_save(EEPROM_STATE_JD_ADDR,STATE_JD);
  //  JD_make();//执行实验函数
    

      if(state == "press")
    {
        lunhuan = !lunhuan;
         BLINKER_LOG("娃哈哈:",lunhuan);
    }
    else
    {
      LED_mode = LED_mode + 1;
      if (LED_mode > 14)
      {
        LED_mode = 0;
      }
      mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
    }
    lamp_twinkle(); //板载信号灯接受指令闪烁一下
}

void button6_callback(const String & state) //电源开关
{
    BLINKER_LOG("APP need set power state: ", state);
    if(state == "on"){
         EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
        SET_RGB( EEPROM.read(EEPROM_LAST_R_ADDR) , EEPROM.read(EEPROM_LAST_G_ADDR) ,\
            EEPROM.read(EEPROM_LAST_B_ADDR) , EEPROM.read(EEPROM_LAST_BRIGHT_ADDR) );   //灯带开机读取RGB 与 亮度 数值 进行显示
 LED_mode = EEPROM.read(EEPROM_RAINBOW_MODE_ADDR);    //读取模式标志位
        RGB1.print(last_r_value,last_g_value,last_b_value,last_bright_value);
//这块加一个滑块条反馈
Slider1.print(cct);
        STATE_power = 1;
        JD_save(EEPROM_STATE_power_ADDR,STATE_power); //保存开关值
        // 反馈开关状态
        Button6.print("on");  //给点灯APP返回信号
        BlinkerMIOT.powerState("on"); // 给小爱返回信号
    }
    else if(state == "off"){    //这里考虑要不要各种标志位清除并 保存？？？？？？？？？
       poweroff_RGB(0,0,0,0);
       last_r_value = 0;
       last_g_value= 0;
       last_b_value= 0;
       last_bright_value= 0;
        RGB1.print(last_r_value,last_g_value,last_b_value,last_bright_value);


LED_mode=0;

        EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
        STATE_power = 0;
        JD_save(EEPROM_STATE_power_ADDR,STATE_power); //保存开关值
        // 反馈开关状态
        Button6.print("off");  //给点灯APP返回信号
        BlinkerMIOT.powerState("off"); // 给小爱返回信号
    }
    lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
void button7_callback(const String & state) //调试开关 我爱你 杠杠
{
    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
  //  BLINKER_LOG("红色值L_R value: ", last_r_value);
  //  BLINKER_LOG("绿色值L_G value: ", last_g_value);
  //  BLINKER_LOG("蓝色值L_B value: ", last_b_value);
  //  BLINKER_LOG("亮度L_Rrightness value: ", last_bright_value); 
   // BLINKER_LOG("色温:", EEPROM.read(EEPROM_COLOR_TEMPERATURE_ADDR));
   // BLINKER_LOG("模式LED_mode: ", LED_mode);
  //  BLINKER_LOG("电脑模式computer_mode:", computer_mode);
    //Blinker.print("millis", "BlinkerTime"); //返回给app调试信息
    lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
void button8_callback(const String & state)//电脑模式变换 加减模式按键
{
     if(state == "press")
    {
        computer_mode = computer_mode - 1;
        if(computer_mode < 4) 
        {
            computer_mode = 17;
        }
    }
     if(state == "tap")
     {
        computer_mode = computer_mode + 1;
        if(computer_mode > 17) 
        {
            computer_mode = 4;
        }
    }
    BLINKER_LOG("computer_mode现在的值:", computer_mode);
    lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
void button9_callback(const String & state)//执行电脑模式
{
    BLINKER_LOG("现在执行电脑模式值computer_mode : ", computer_mode);
    LED_mode = computer_mode;
    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
    mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
    lamp_twinkle(); //板载信号灯接受指令闪烁一下
}
void button10_callback(const String & state)//执行电视模式
{
//LEDS.addLeds<LED_TYPE, PIN, COLOR_ORDER>(leds, NUMPIXELS);
//FastLED.setBrightness(150);
LED_mode = 17;
EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址 EEPROM_OPERAT_MAX_ADDR
    mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
    lamp_twinkle(); //板载信号灯接受指令闪烁一下

}
                              /*小爱回调函数*/
//小爱设置颜色回调
void miotColor(int32_t color){
    uint8_t colorR;uint8_t colorG;uint8_t colorB;

    BLINKER_LOG("need set color: ", color);
    colorR = color >> 16 & 0xFF;
    colorG = color >>  8 & 0xFF;
    colorB = color       & 0xFF;

    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址addr
    SET_RGB( colorR , colorG , colorB , EEPROM.read(EEPROM_LAST_BRIGHT_ADDR) );  

    BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);

    BlinkerMIOT.color(color);
    BlinkerMIOT.print();    //属性发送出去
RGB1.print(last_r_value,last_g_value,last_b_value,last_bright_value);   //上传RGB亮度信息

duringdeal();  //不中断模式调节
}

//小爱设置亮度回调
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
 int colorLD = bright.toInt();
BLINKER_LOG("now set brightness: ", colorLD);
last_bright_value = colorLD*2.55;
EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址addr
    JD_save(EEPROM_LAST_BRIGHT_ADDR,last_bright_value); //保存亮度值

        SET_RGB( EEPROM.read(EEPROM_LAST_R_ADDR) , EEPROM.read(EEPROM_LAST_G_ADDR) ,\
            EEPROM.read(EEPROM_LAST_B_ADDR) , EEPROM.read(EEPROM_LAST_BRIGHT_ADDR) ); 
    
    
FastLED.setBrightness(last_bright_value);
  ws2812fx.setBrightness(last_bright_value);
Serial.printf("亮度调节中...%d",colorLD);
BlinkerMIOT.brightness(colorLD);//反馈小爱控制状态
BlinkerMIOT.print();
}

//小爱设置色温回调
void miotColoTemp(int32_t colorTemp)
{
    BLINKER_LOG("小爱色温:", colorTemp);
    slider1_callback(colorTemp);
    Slider1.print(cct);  //发送用户需要的滑动条数值及设置的颜色到APP
    BlinkerMIOT.colorTemp(colorTemp);
    BlinkerMIOT.print();
}

//小爱设置模式回调
void miotMode(uint8_t mode)
{
    BLINKER_LOG("need set mode: ", mode);

    if (mode == BLINKER_CMD_MIOT_DAY) {
        button1_callback("on");//日光模式 按键模式1
    }
    else if (mode == BLINKER_CMD_MIOT_NIGHT) {
        button2_callback("on");//夜光模式 按键模式1
    }
    else if (mode == BLINKER_CMD_MIOT_COLOR) {
        button4_callback("on");  //彩光模式
    }
    else if (mode == BLINKER_CMD_MIOT_WARMTH) {
        button5_callback("on");//温馨模式 JD
  




     
      
    }
    else if (mode == BLINKER_CMD_MIOT_TV) {
        button10_callback("on");//电视模式  律动
    }
    else if (mode == BLINKER_CMD_MIOT_READING) {
       button3_callback("on");//阅读模式 模式2
    }
    else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
        button9_callback("on");//电脑模式  变换风格
    }

    BlinkerMIOT.mode(mode);
    BlinkerMIOT.print();
}

//小爱电源回调
void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) 
    {
        button6_callback("on");
    }
    else if (state == BLINKER_CMD_OFF) 
    {
        button6_callback("off");
    }
    BlinkerMIOT.print();
}
                          /*初始化函数*/
void setup() {
    // 初始化串口
    Serial.begin(115200);
    Serial.println();
ws2812fx.init();
  
  ws2812fx.setSpeed(100);
  ws2812fx.setSegment(0, NUMPIXELS,NUMPIXELS,FX_MODE_RAINBOW_CYCLE,RED, 2500, false);
  ws2812fx.start();
    FastLED.addLeds<LED_TYPE, PIN, COLOR_ORDER>(leds, NUMPIXELS);
    // WS2812初始化
  //  pixels.begin();//
    pinMode(PIN, OUTPUT);
    pinMode(press, INPUT_PULLUP);
    pinMode(nextpress, INPUT_PULLUP);
    pinMode(austpress, INPUT_PULLUP); 
    pinMode(STATE_LED, OUTPUT);
    pinMode(OUT_JD, OUTPUT);
    pinMode(OUT_canc, OUTPUT); //能够操作指示灯
    digitalWrite(STATE_LED, LOW);   //打开WIFI指示灯
    digitalWrite(OUT_canc, LOW);
   loading();//开机效果
    EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR
    //last_bright_value = EEPROM.read(EEPROM_LAST_BRIGHT_ADDR); //开机读取亮度值
    //last_r_value =  EEPROM.read(EEPROM_LAST_R_ADDR) ;
    //last_g_value =EEPROM.read(EEPROM_LAST_G_ADDR);
    //last_b_value = EEPROM.read(EEPROM_LAST_B_ADDR);
    
 

 //SET_RGB( EEPROM.read(EEPROM_LAST_R_ADDR) , EEPROM.read(EEPROM_LAST_G_ADDR) ,\
        EEPROM.read(EEPROM_LAST_B_ADDR) , EEPROM.read(EEPROM_LAST_BRIGHT_ADDR));  
        delta = int(last_bright_value / mid); //渐变量
        
        FastLED.setBrightness(last_bright_value);

ws2812fx.setBrightness(last_bright_value);
        
/*---------------------如果实际出现 电源在关的状态下重启芯片 灯带闪烁一下 又灭，删除上边SET_RGB调用就应该行了-----*/

LED_mode = EEPROM.read(EEPROM_RAINBOW_MODE_ADDR);      


    cct = EEPROM.read(EEPROM_COLOR_TEMPERATURE_ADDR)*43; //读取色温值 掉电保存最大值256 *43系数接近设置的色温值
    
num_ = EEPROM.read(EEPROM_AU_ST_ADDR);
    STATE_JD = EEPROM.read(EEPROM_STATE_JD_ADDR);//读取掉电保存值
    STATE_power = EEPROM.read(EEPROM_STATE_power_ADDR);//读取掉电保存值

     JD_make();//在初始化读取掉点保存的数值后，执行一遍函数 包括电源方面的函数，

    //打开blinker调试
    #if defined(BLINKER_PRINT)
        BLINKER_DEBUG.stream(BLINKER_PRINT);
    #endif

    #if defined(Wifi_DIRECT)    //WIFI直连
        Blinker.begin(auth,ssid,pswd);
    #else
        //Web配网
        webconnect();
        // 初始化blinker
        Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
    #endif


    digitalWrite(STATE_LED, HIGH);  ///连接成功,关闭WIFI指示灯

    Blinker.attachData(dataRead);
    // 注册调节颜色的回调函数
    RGB1.attach(rgb1_callback);
    // 注册滑动条的回调函数
    Slider1.attach(slider1_callback);
    // 注册按键的回调函数
    Button1.attach(button1_callback);
    Button2.attach(button2_callback);
    Button3.attach(button3_callback);
    Button4.attach(button4_callback);
    Button5.attach(button5_callback);
    Button6.attach(button6_callback);
    Button7.attach(button7_callback);
    Button8.attach(button8_callback);
    Button9.attach(button9_callback);
    Button10.attach(button10_callback);
    // 注册心跳包函数
    Blinker.attachHeartbeat(heartbeat);
    //小爱调节RGB亮度
    BlinkerMIOT.attachBrightness(miotBright);
    // 注册小爱设置颜色函数
    BlinkerMIOT.attachColor(miotColor);
    // 注册小爱设置色温函数
    BlinkerMIOT.attachColorTemperature(miotColoTemp);
    // 注册小爱设置模式函数
    BlinkerMIOT.attachMode(miotMode);
    // 注册小爱电源函数
    BlinkerMIOT.attachPowerState(miotPowerState);
    heartbeat();
    Text1.print(LED_mode, computer_mode);
}
//循环
void loop() {
 if((millis()-time_lunhuan>10000) && lunhuan == 1)
 {
   LED_mode = LED_mode + 1;
      if (LED_mode > 14)
      {
        LED_mode = 0;
      }
      BLINKER_LOG("娃哈哈:",lunhuan);
      time_lunhuan=millis();
      mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
      Text1.print(LED_mode, computer_mode);
 }

    if (digitalRead(nextpress)==LOW)   //按下下一个 模式按键 ，切换模式 并且保存模式
    {
        LED_mode = LED_mode + 1;
        if (LED_mode == 12)
        {
            LED_mode = 0;
        }
         BLINKER_LOG("正在跑:",LED_mode);
        Text1.print(LED_mode, computer_mode);
        mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
    }
    
    if(digitalRead(press)==LOW)  //检测按键  如果按下 则设置模式为0  为的是 执行一些模式的时候 同时执行blinker联网函数时有卡顿，
    {
        LED_mode = 0;
        EEPROM.begin(EEPROM_OPERAT_MAX_ADDR); //申请操作到地址EEPROM_OPERAT_MAX_ADDR 
        SET_RGB( EEPROM.read(EEPROM_LAST_R_ADDR) , EEPROM.read(EEPROM_LAST_G_ADDR) ,\
        EEPROM.read(EEPROM_LAST_B_ADDR) ,EEPROM.read(EEPROM_LAST_BRIGHT_ADDR));  
        Text1.print(LED_mode, computer_mode);
        digitalWrite(STATE_LED, HIGH);
        mode_save(EEPROM_RAINBOW_MODE_ADDR,LED_mode);
    }
    if(LED_mode != 17 && LED_mode != 15 && LED_mode != 16)   //此模式下 不进行联网，
    { 
       Blinker.run();
    }

    if (digitalRead(STATE_LED) == HIGH && (LED_mode ==3 || LED_mode ==8 || LED_mode ==15 || LED_mode ==16 || LED_mode ==17))
    {
      digitalWrite(STATE_LED, LOW);  //打开不受控制指示灯
    }

    //循环检测 对应模式（彩虹模式/亮度模式）标志位是否为1，是则执行对应模式
switch (LED_mode)
 {

case 1:bright_mode_set(1);
break;
case 2:
CHHXD();//
break;
case 3:
if (digitalRead(austpress) == LOW)
{
 
  if ((millis() - aust_time > 1500) && (num_ <30) )
  {
    num_ = num_ + 30;
    BLINKER_LOG("num1111现在是", num_);
    JD_save(EEPROM_AU_ST_ADDR,num_);
     aust_time = millis();
  }
  if ((millis() - aust_time > 1500) && (num_ >=30) )
  {
    if (num_ > 60)
    {
    num_ = 0;
    }
    else
    {num_ = num_ - 30;}
    BLINKER_LOG("num2222现在是", num_);
    JD_save(EEPROM_AU_ST_ADDR,num_);
     aust_time = millis();
  }

  
  
}
style_switch();
 
break;
case 4:
        colorWipe(pixels.Color(255, 0, 0), 20);// Red
        colorWipe(pixels.Color(0, 255, 0), 20);// Green
         colorWipe(pixels.Color(0, 0, 255), 20);// Blue
        // colorWipe(pixels.Color(255, 255, 255), 20);// 白
         colorWipe(pixels.Color(128, 0, 128), 20);// 紫色
         colorWipe(pixels.Color(255, 255, 0), 20);// 黄
         colorWipe(pixels.Color(0, 255, 255), 20);// 青
          colorWipe(pixels.Color(255, 0, 255), 20);// Blue
         colorWipe(pixels.Color(random(10,200), random(20,180), random(20,190)), 20);// Blue

break;
case 5:theaterChase(pixels.Color(EEPROM.read(EEPROM_LAST_R_ADDR), EEPROM.read(EEPROM_LAST_G_ADDR), EEPROM.read(EEPROM_LAST_B_ADDR)), 100);
break;
case 6:rainbowCycle(3);
break;
case 7:theaterChaseRainbow(10);
break;
case 8:
theaterChase(pixels.Color(127, 127, 127), 50); // White
      theaterChase(pixels.Color(255, 0, 0), 50); // Red
      theaterChase(pixels.Color(0, 0, 255), 50); // Blue剧院追逐
      theaterChase(pixels.Color(0, 255, 0), 50);
      theaterChase(pixels.Color(255, 0, 255), 50);
      theaterChase(pixels.Color(255, 255, 0), 50);
      theaterChase(pixels.Color(0, 255, 255), 50);
      
break;
case 9:confetti();


break;
case 10:LSD();//
break;
case 11:rainbow();
break;
case 12:sinelon();
break;
case 13: bpm();
break;
case 14:rainbowWithGlitter();
break;
case 15:juggle();

break;
case 16:lvdong2();
break;
case 17:
if (millis() - resetTime > 10000)//每隔30S重新计算均值 30000
  {
    int sig_min = 1024;
    int sig_max = 0;
  }
  musicV();


break;
 }
}

//Web配网
void webconnect() {  

  WiFiManager wifiManager;  //实例化WiFiManager
  wifiManager.setDebugOutput(true); //打开Debug
  //wifiManager.setConnectTimeout(10); //设置超时
  wifiManager.setHeadImgBase64(FPSTR(Icon)); //设置图标
  wifiManager.setPageTitle("季鑫杨的WiFi配置页");  //设置页标题

  if (!wifiManager.autoConnect("JXYDSG")) {  //AP模式
    Serial.println("连接失败并超时");
    //重新设置并再试一次，或者让它进入深度睡眠状态
    ESP.restart();
    delay(1000);
  }
  digitalWrite(STATE_LED, HIGH);
  Serial.println("connected...^_^");
  yield();
}

/*******************************************************************************
*@ Description    将RGB转换成色温（CCT）  
*@ Input          :<tmpKelvin>输入的色温值（[1000,40000]单位：K，开尔文）
*@ Output         :
*@ Return         :
*******************************************************************************/
int Color_temperature_conversion(unsigned int tmpKelvin,unsigned char* r,unsigned char* g,unsigned char* b){
 
    static double tmpCalc = 0;
    
    if(tmpKelvin < 1000){
        tmpKelvin = 1000;
    }
    if(tmpKelvin > 40000){
        tmpKelvin = 40000;
    }
    tmpKelvin = tmpKelvin/100;
    
    /*----#red----------------------------------------------------------------------------*/
    if(tmpKelvin <= 66){
        *r = 255;
    }
    else{
        tmpCalc = tmpKelvin - 60;
        tmpCalc = 329.698727446 * (pow(tmpCalc,-0.1332047592));
        *r = tmpCalc;
        if(*r <0){
            *r = 0;
        }
    
        if(*r > 255){
            *r = 255;
        }
    }
    
    /*----#green----------------------------------------------------------------------------*/
    if(tmpKelvin <= 66){
        tmpCalc = tmpKelvin;
        tmpCalc = 99.4708025861 * log(tmpCalc) - 161.1195681661;
        *g = tmpCalc;
    
        if(*g < 0){
            *g = 0;
        }
        if(*g > 255){
            *g = 255;
        }    
    }
    else{
        tmpCalc = tmpKelvin - 60;
        tmpCalc = 288.1221695283 * (pow(tmpCalc,-0.0755148492));
        *g = tmpCalc;
    
        if(*g < 0){
            *g = 0;
        }
        if(*g > 255){
            *g = 255;
        }    
    }
    
    /*----#blue----------------------------------------------------------------------------*/
    if(tmpKelvin >= 66){
        *b = 255;
    }
    else if(tmpKelvin <= 19){
        *b = 0;
    }
    else{
        tmpCalc = tmpKelvin - 10; 
        tmpCalc = 138.5177312231 * log(tmpCalc) - 305.0447927307;
        *b = tmpCalc;
    
        if(*b < 0){
            *b = 0;
        }
        if(*b > 255){
            *b = 255;
        }
    }
    return 0;
}
//模式2
void rainbow() 
{
  // FastLED's built-in rainbow generator
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  fill_rainbow( leds, NUMPIXELS, gHue, 7);
  FastLED.show();
 // BLINKER_LOG("正在跑");
 FastLED.delay(8); 

}

//模式1
void bright_mode_set(int ms){
    static int shortbright;
    static int bb ;
   // b最大设置的是255
     shortbright = EEPROM.read(EEPROM_LAST_BRIGHT_ADDR);
     if (shortbright < 85)
     {
         bb = 1;
     }
     else if (shortbright > 85 && shortbright < 170) {bb = 2;}
     else {bb=3;}
     
    for(int b=0; (b< shortbright ); b+=bb){
        // 把灯条变色
        for (uint16_t i = 0; i < NUMPIXELS; i++)
        {
            pixels.setPixelColor(i,last_r_value,last_g_value,last_b_value);
        }
        // 设置亮度
        pixels.setBrightness(b);
        // 送出显示
        pixels.show();
        Blinker.run();
        // delay(ms);
    }
    for(int b=EEPROM.read(EEPROM_LAST_BRIGHT_ADDR); (b>0) && (LED_mode == 1) ; b-=bb){
        // 把灯条变色
        for (uint16_t i = 0; i < NUMPIXELS; i++)
        {
            pixels.setPixelColor(i,last_r_value,last_g_value,last_b_value);
        }
        // 设置亮度
        pixels.setBrightness(b);
        // 送出显示
        pixels.show();
        Blinker.run();
        // delay(ms);
    }
}

//模式8调用函数  模式12
void theaterChase(uint32_t c, uint8_t wait) 
{
  for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
    for (int q = 0; q < 3; q++) {
      for (uint16_t i = 0; i < pixels.numPixels(); i = i + 3) {
        pixels.setPixelColor(i + q, c);  //turn every third pixel on
      }
      pixels.show();
      delay(wait);

      for (uint16_t i = 0; i < pixels.numPixels(); i = i + 3) {
        pixels.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
  }
}
//模式4调用函数
void colorWipe(uint32_t c, uint8_t wait) {
    
 for (uint16_t i = 0; i < pixels.numPixels() && LED_mode == 4; i++) {
    pixels.setPixelColor(i, c);
    pixels.show();
    delay(wait);
    Blinker.run();//
 }  
  }

//模式6
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    if (LED_mode != 6) break;
    Blinker.run();
    for (i = 0; i < pixels.numPixels(); i++) {
      if (LED_mode != 6) break;
      Blinker.run();
      pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
    }
    pixels.show();
    delay(wait);
  }
}
//模式7
void theaterChaseRainbow(uint8_t wait) {

  for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
   if (LED_mode != 7 )break;
    Blinker.run();
    for (int q = 0; q < 3; q++) {
      if (LED_mode != 7) break;
      Blinker.run();
      for (uint16_t i = 0; i < pixels.numPixels(); i = i + 3) {
       if (LED_mode != 7) break;
        Blinker.run();
        pixels.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
      }
      pixels.show();
      delay(wait);

      for (uint16_t i = 0; i < pixels.numPixels(); i = i + 3) {
        if (LED_mode != 7) break;
        Blinker.run();
        pixels.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
  }
}
//模式10
void LSD() {
  for (int i = 0; i < NUMPIXELS; i++) {
    leds[i] = CRGB(last_r_value,last_g_value,last_b_value);
    FastLED.show();
    delay(8);
    leds[i] = CRGB::Black;
    FastLED.show();
   // delay(2);
  }
}
//模式11
void CHHXD() {
  for (int i = 0; i < NUMPIXELS; i++) {
    leds[i] = CHSV( (hue + (255 / NUMPIXELS) * i), 255, last_bright_value); //用HSV色彩空间，不断改变H即可
    FastLED.show();
  }
  delay(2);
  hue = (hue + 3) % 255;
}
//悉尼龙 模式 12
void sinelon()
{
  // a colored dot sweeping back and forth, with fading trails
  fadeToBlackBy( leds, NUMPIXELS, 20);
  int pos = beatsin16( 13, 0, NUMPIXELS-1 );
  leds[pos] += CHSV( gHue, 255, last_bright_value);
  FastLED.show();
  FastLED.delay(9); 
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  //Serial.printf("ghue值是",gHue);
}
// 模式 13
void bpm()
{
  // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  uint8_t BeatsPerMinute = 62;
  CRGBPalette16 palette = PartyColors_p;
  uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  for( int i = 0; i < NUMPIXELS; i++) { //9948
    leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  }
  FastLED.show();
  FastLED.delay(9); 
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
}
//模式14
void rainbowWithGlitter() 
{
  // built-in FastLED rainbow, plus some random sparkly glitter
  rainbow();
  addGlitter(80);
  FastLED.show();
  FastLED.delay(9); 
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
}
//模式15
void juggle() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUMPIXELS, 20);
  byte dothue = 0;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16( i+7, 0, NUMPIXELS-1 )] |= CHSV(dothue, 200, last_bright_value);
    dothue += 32;
    FastLED.show();
  FastLED.delay(9); 
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  }
}
//律动2 模式16

void lvdong2() {
  // 将主代码放在此处，以便重复运行：
//读取音频信号并对其进行过滤
  int n, height;
  n = analogRead(sigPin);
  // 移除1.25VDC的MX9614偏置
  n = abs(1023 - n);
  // 硬限制噪音/嗡嗡声
  n = (n <= NOISE) ? 0 : abs(n - NOISE);
  // 应用指数滤波器平滑原始信号
  ADCFilter.Filter(n);
  lvl = ADCFilter.Current();
  // 绘制原始信号与过滤信号的对比图
  //Serial.println(n);
  //Serial.print(" ");
  //Serial.println(lvl);
  // 以范围的百分比计算像素数

//待办事项：可以通过使用最小/最大音频级别的运行平均值动态完成
  height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
  if(height < 0L) height = 0;
  else if(height > TOP) height = TOP;
  // turn the LEDs corresponding to the level on/off
  for(uint8_t i = 0; i < NUMPIXELS; i++) {
    // turn off LEDs above the current level
    if(i >= height) leds[i] = CRGB(0,0,0);
    // otherwise, turn them on!
    else leds[i] = Wheel2( map( i, 0, NUMPIXELS-1, 30, last_bright_value ) );
  }
  FastLED.show();
}

//碎纸屑模式 9
void confetti() 
{
  // random colored speckles that blink in and fade smoothly
  fadeToBlackBy( leds, NUMPIXELS, 10);
  int pos = random16(NUMPIXELS);
  leds[pos] += CHSV( gHue + random8(64), 200, 255);
  FastLED.show();
  FastLED.delay(9); 
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
}
//附加函数
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

void addGlitter( fract8 chanceOfGlitter) 
{
  if( random8() < chanceOfGlitter) {
    leds[ random16(NUMPIXELS) ] += CRGB::White;
  }
}

CRGB Wheel2(byte WheelPos) {   //第二种律动模式的函数
  // return a color value based on an input value between 0 and 255
  if(WheelPos < 85)
    return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}
/*---------------律动1相关函数-------------模式3----*/
void musicV()  
{
 
  //计算环境读取最大最小值，所以在上电后循环几次后达到平稳的触发值。
  sig = analogRead(sigPin);
 
 // Serial.println(sig);
  if (sig > sig_max)//
  {
    sig_max = sig;
  }
  if (sig < sig_min)
  {
    sig_min = sig;
  }
  int mean = (sig_min + sig_max) / 2;
  int _sig = abs(sig - mean ) + mean;
  _sig = constrain(_sig, int(mean * triger_factor), sig_max);
  //Serial.println(_sig);
  si = map(_sig - int(mean * triger_factor), int(mean * triger_factor) - int(mean * triger_factor), int(sig_max) - int(mean * triger_factor), 0, mid - 2);
  si = constrain(si, 0, mid - 2);
  //Serial.println(si);
  if (si == mid - 2) {
    randC = random(0, 255);//灯冲到顶时掉落珠的颜色变化
    pre_si = 0;
  }
  //===================================================================================================== PART1
  if (si > pre_si)
  {
    if (si > drop_dot)
    {
      if (si * 1.6  < mid - 1) {
        drop_dot = int(si * 1.6);//弹性
      }
      else {
        drop_dot = mid - 1;//顶端
      }
    }
    for (int j = pre_si; j < si + 1; j++)
    {
      while ( millis() - triger_time < int(interval / (si - pre_si) + 2))
      {
          
        if ((drop_dot > j + 3 ) && (millis() - drop_time > drop_interval))//间隔3个灯珠
        {
          leds [mid + drop_dot + 1] = CRGB::Black;
          leds [mid - 1 - drop_dot - 1] = CRGB::Black;
          leds[mid + drop_dot] = CHSV(randC, 255, last_bright_value);
          leds[mid - 1 - drop_dot] = CHSV(randC, 255, last_bright_value);
          FastLED.show();
          drop_dot--;
          drop_time = millis();
        }
      }
      leds [mid + j] = CHSV(delta * j, 255, last_bright_value - delta * j);//在此处改灯珠颜色 此处渐变   亮度渐变
      leds [mid - 1 - j] = CHSV(delta * j, 255, last_bright_value - delta * j);
      //leds [mid + j] = CHSV(randC, 255, 255 - delta * j);   //此处固定颜色 亮度渐变
      //leds [mid - 1 - j] = CHSV(randC, 255, 255 - delta * j);
      FastLED.show();
      triger_time = millis();
    }
  }
  // --------------------------------------------------------------------------------------------------
  //===================================================================================================== PART2
  if (si < pre_si)
  {
    for (int k = pre_si; k > si - 1; k--)
    {
      while ( millis() - triger_time < int(interval / (pre_si - si) + 2))
      {
          
        if ((drop_dot > k + 3 ) && (millis() - drop_time > drop_interval))
        {
          leds [mid + drop_dot + 1] = CRGB::Black;
          leds [mid - 1 - drop_dot - 1] = CRGB::Black;
          leds[mid + drop_dot] = CHSV(randC, 255, last_bright_value - delta * k);
          leds[mid - 1 - drop_dot] = CHSV(randC, 255, last_bright_value - delta * k);
          FastLED.show();
          drop_dot--;
          drop_time = millis();
        }
      }
      leds[mid + k + 1] = CRGB::Black;
      leds[mid - 1 - k - 1] = CRGB::Black;
      FastLED.show();
      triger_time = millis();
    }
  }
  // --------------------------------------------------------------------------------------------------
  if (si == 0 && pre_si == 0 && drop_dot > -1 && (millis() - drop_time > drop_interval))  //当信号为0的时候 归零 熄灯
  {
    leds [mid + drop_dot + 1] = CRGB::Black;
    leds [mid - 1 - drop_dot - 1] = CRGB::Black;
    leds[mid + drop_dot] = CHSV(randC, 255, last_bright_value);
    leds[mid - 1 - drop_dot] = CHSV(randC, 255, last_bright_value);
    if (drop_dot == 0)
    {
      leds [mid + drop_dot] = CRGB::Black;
      leds [mid - 1 - drop_dot] = CRGB::Black;
    }
    FastLED.show();
    drop_dot--;
    drop_time = millis();
  }
  pre_si = si;
}

/*---------------律动相关函数-----------------*/
void loading(){
  int remain,rand_rgb;
  remain=NUMPIXELS;
  for(int i=0;i<NUMPIXELS;i++){
    leds[NUMPIXELS-1]=CHSV(random(10, 255), 255, 200);
    for(int j=NUMPIXELS-1;j>i;j--){
      leds[j-1]=leds[j];
      leds[j]=CRGB::Black;
      FastLED.show();
      delay(2);
    }
  }
  
}
//模式17
void style_switch(){
  static int num_1 ;
  static int ss = 1;
  num_1 = num_ -30;
  if (ss == 1)
  {
    ws2812fx.stop();
      //ws2812fx.setMode(RGB_style[num_]);
      ws2812fx.setSegment(0, 0,NUMPIXELS,RGB_style[num_1],RED, 2000, false);
      ws2812fx.start();
      ss=0;
  }
  


   if((millis()-time_count>8000) && (num_ < 30)){//8秒换一种灯效  在不是模式17的固定模式下 才会8s切换一个
      ws2812fx.stop();
      //ws2812fx.setMode(RGB_style[num_]);
      ws2812fx.setSegment(0, 0,NUMPIXELS,RGB_style[num_],RED, 2000, false);
      num_++;
      ws2812fx.start();
      if(num_==24) num_=0;
      time_count=millis();
      BLINKER_LOG("num现在是", num_);
   }
   ws2812fx.service();
}
